<?php
@session_start();
include '../Security/CheckAdmin.php';
include '../Home/DefineVar.php';
include '../DataBase/ConnectDB.php';

 mysql_select_db(MYSQL_DATABASE,$link);
 
/**
 * ユーザの月単位での会議室使用料
 * @param $time1
 * @param $time2
 * @param $link
 * @param $accountName
 * @return unknown_type
 */
function FeeByMonth($time1,$time2,$link,$accountName){
	$arrayFee = array(0,0,0,0,0);
	
//	print($time1."/".$time2."<br>");
	//Tinh phi su dung va phat
	$result = mysql_query("SELECT orderstatus, fee FROM ".MYSQL_TABLE_ORDER." WHERE  accountname = '".$accountName."' AND orderstatus !='0' AND starttime >= '".$time1."' AND starttime < '".$time2."'",$link);
if(mysql_num_rows($result)!=0){
while($row=mysql_fetch_array($result)){
	if($row[0] ==1) $arrayFee[0] +=$row[1];
	if($row[0]==2) $arrayFee[1]+=$row[1];
}
}

//Tinh phi dat phong
$result = mysql_query("SELECT orderfee FROM ".MYSQL_TABLE_ORDER." WHERE accountname = '".$accountName."' AND orderstatus !='2' AND ordertime >= '".$time1."' AND ordertime < '".$time2."'",$link);
if(mysql_num_rows($result)!=0){
while($row=mysql_fetch_array($result)){
	$arrayFee[2] +=$row[0];
}
}

//Tinh phi cancel phong
$result = mysql_query("SELECT fee FROM ".MYSQL_TABLE_ORDER." WHERE accountname = '".$accountName."' AND  orderstatus ='0' AND canceltime >= '".$time1."' AND canceltime < '".$time2."'",$link);
if(mysql_num_rows($result)!=0){
while($row=mysql_fetch_array($result)){
	$arrayFee[3] +=$row[0];
}
}

$arrayFee[4]=$arrayFee[0]+$arrayFee[1]+$arrayFee[2]+$arrayFee[3];
return $arrayFee;
}

$yearMonth=explode("/",$_GET["yearMonth"]);
$yearMonth1 =mktime(0,0,0,$yearMonth[1],1,$yearMonth[0]);
$yearMonth2 = mktime(0,0,0,$yearMonth[1]+1,1,$yearMonth[0]);
//echo date("Y/n/j");
$namefile = CODE_SYSTEM."-".$yearMonth[0]."-".$yearMonth[1].".CSV";

$folder = FOLDER_SYSTEM."/".FOLDER_NAME."/".$yearMonth[0];
//echo $folder;
//echo "<br>".$yearMonth[1]."<br>";
//$command ="c:\windows\system32\cmd.exe mkdir ".$folder;
if(@mkdir($folder,0777,true)== false){
	//echo CRE_FOL_ERR_MESS;
	//exit();
}
//データをCVSファイルに記録する
$fpt = fopen($folder."\\".$namefile,"w");
if($fpt==NULL){
	print("<br><script type='text/javascript'> alert('".CRE_CSV_ERR_MESS."'); window.location ='CreateCSV.php';</script> ");
	exit();
}
$result = mysql_query("SELECT name FROM ".MYSQL_TABLE_USERINFOR." WHERE accountname ='".$_SESSION['admin']."'",$link);
$row = mysql_fetch_array($result);

$line1 = SYS_ID.",".$yearMonth[0].",".$yearMonth[1].",".date('Y').",".date('n').",".date('j').",".date('G').",".(int)date('i').",".(int)date('s');
$line1 = $line1.",".$_SESSION['admin'].",".$row[0];
fwrite($fpt,$line1."\n");

$result = mysql_query("SELECT accountname, name, address, phone FROM ".MYSQL_TABLE_USERINFOR,$link);
if(!$result){
	print("<br><script type='text/javascript'> alert('".SQL_ERR_MESS."'); window.location ='CreateCSV.php';</script> ");
	exit();
}
while($row=mysql_fetch_array($result)){
	$arrayFee=FeeByMonth(date("Y-m-d G:i:s",$yearMonth1),date("Y-m-d G:i:s",$yearMonth2),$link,$row[0]);
	if($arrayFee[4]!=0){
		$line2= $row[0].",".$row[1].",".$arrayFee[4].",".str_replace(","," ",$row[2]).",".$row[3];
		fwrite($fpt,$line2."\n");
		//echo trim($row[2], "d,")."<br>";
	}
}

$line3 = "END__END__END".",".$yearMonth[0].",".$yearMonth[1];
fwrite($fpt,$line3."\n");
print("<br><script type='text/javascript'> alert('".CRE_CSV_OK_MESS."'); window.location ='CreateCSV.php';</script> ");
?>